<%@ page import="com.example.softwareconfigurationcoursedesign.ShopPage.Product" %>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="com.example.softwareconfigurationcoursedesign.ShopPage.ProductService" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="jakarta.servlet.http.Cookie" %>
<%@ page import="java.net.URLDecoder" %>
<%@ page import="com.google.gson.reflect.TypeToken" %>
<%@ page import="com.google.gson.Gson" %>
<%@ page import="java.nio.charset.StandardCharsets" %>

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta content="IE=edge" http-equiv="X-UA-Compatible">
    <meta content="width=device-width, initial-scale=1.0" name="viewport">
    <link href="CSS/Shop.css" rel="stylesheet">
    <title>购物车</title>
    <style>


        #snackbar {
            visibility: hidden;
            min-width: 250px;
            margin-left: -125px;
            background-color: #333;
            color: #fff;
            text-align: center;
            border-radius: 2px;
            padding: 16px;
            position: fixed;
            z-index: 1;
            left: 50%;
            bottom: 30px;
        }

        #animationBox {
            width: 500px;
            height: 70px;
            background: linear-gradient(to right, #fea2a2, #8db3f9, #65cea6, #5167d3);
            position: absolute; /* 绝对定位 */
            top: 0;
            left: 0;
            text-align: center;
            /*line-height: 100px; !* 垂直居中文本 *!*/
            color: white;
            font-weight: bold;
            font-size: 16px;
            display: none;
        }

        /* 下落和上升动画 */
        @keyframes dropAndRise {
            0% {
                opacity: 0;
                transform: translateY(-100%);
            }
            50% {
                opacity: 1;
            }
            100% {
                transform: translateY(0%);
                opacity: 0;
            }
        }

        table {
            width: 100%;
            border-collapse: collapse;
        }

        th, td {
            padding: 10px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }

        .product_img {
            max-width: 100px;
            max-height: 100px;
        }
    </style>
</head>
<body>
<h1><button onclick="window.location.href='shop.jsp'">< 点击返回</button>  <br>购物车</h1>
<table>
    <tr>
        <th>图片</th>
        <th>名称</th>
        <th>价格</th>
        <th>数量</th>
        <th>操作</th> <!-- 添加操作列 -->
    </tr>
    <%-- Java代码部分，获取购物车中的商品信息并展示 --%>
    <%
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            String jdbcUrl = "jdbc:mysql://localhost:3306/course_design";
            String username = "root";
            String password = "123456";
            DriverManager.getConnection(jdbcUrl, username, password);
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }
    %>
    <%
        List<String> cartItems = new ArrayList<>();
        if (request.getCookies() != null) {
            for (Cookie cookie : request.getCookies()) {
                if (cookie.getName().equals("cart")) {
                    String cartJSON = URLDecoder.decode(cookie.getValue(), StandardCharsets.UTF_8);
                    cartItems = new Gson().fromJson(cartJSON, new TypeToken<List<String>>(){}.getType());
                    break;
                }
            }
        }

        ProductService productService = new ProductService();
        double totalPrice = 0.0;
    %>
    <% for (String itemName : cartItems) { %>
    <% List<Product> productList = productService.getProductListCarFromDatabase(itemName); %>
    <% for (Product product : productList) { %>
    <tr>
        <td>
            <img src="<%= product.getPurl() %>" alt="<%= product.getPname() %>" class="product_img">
        </td>
        <td><%= product.getPname() %></td>
        <td>￥<%= product.getPprice() %></td>
        <td>1</td>
        <td>
            <button onclick="removeItem('<%= product.getPname() %>')">移除</button> <!-- 添加移除按钮 -->
        </td>
    </tr>
    <% totalPrice += product.getPprice(); %>
    <% } %>
    <% } %>

    <tr>
        <td colspan="3"></td>
        <td>商品总价：￥<span id="totalPrice"><%= totalPrice %></span></td>
    </tr>
    <tr>
        <td colspan="4">
            <button onclick="clearCart()">清空购物车</button>
            <button onclick="refreshPage()">刷新页面</button>
        </td>
        <td>
            <button onclick="settleCount(<%=totalPrice%>)">结算</button>
        </td>
    </tr>

</table>
</body>
<script src="JavaScript/ShopCar.js">

</script>

</html>
